Programming Questions for Practice
You are given an array of positive integers, arr, of size array_length. How many elements in array arr are perfectly divisible by either the product or sum of their digits?
#include <stdio.h>
#include <stdlib.h>
/*
You are given an array of positive integers, arr, of size array_length.
How many elements in array arr are perfectly divisible by either the
product or sum of their digits?
*/
int numDivisibleBySumOrProductOfDigits(int array_length, int *arr) {
int i, counter = 0, sum = 0, product = 1, digit, temp;
for(i = 0; i < array_length; i++) {
temp = arr[i]; sum = 0; product = 1;
while(temp != 0) {
digit = temp % 10;
sum = sum + digit;
product = product * digit;
temp = temp / 10;
}
if(product != 0) {
if((arr[i]%sum)==0 || (arr[i]%product)==0)
counter++;
} else {
if(arr[i]%sum == 0)
counter++;
}
}
return counter;
}
int main() {
int arr[] = {25, 36, 45};
int length = sizeof(arr)/sizeof(arr[0]);
int result = numDivisibleBySumOrProductOfDigits(length, arr);
printf("%d", result);
return 0;
}
Code not available...
Code not available...